// http://mobile.tutsplus.com/tutorials/android/ndk-tutorial/

package ar.com.blitzsoftware.footgolfer;

import android.app.Activity;
import android.maps.MapActivity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


class MyLocationListener implements LocationListener
{
	private static final String TAG = "Footgolfer";
	
	private Location _lastKnownLocation = null;
	
	public Location getLastKnownLocation()
	{
		if (_lastKnownLocation == null)
		{
			Log.v(TAG, "getLastKnownLocation - NULL");
		}
		
		return _lastKnownLocation;
	}
	
	public void onLocationChanged(Location location)
	{
		//Log.v(TAG, "onLocationChanged - 1");
		
		//String locationText = "Location: " + "Lat = " + location.getLatitude() + "Lon = " + location.getLongitude();

		if (isBetterLocation(location, _lastKnownLocation))
		{
			//Log.v(TAG, "onLocationChanged - 2");
			
			_lastKnownLocation = location;
			//locationText += " - BETTER LOCATION!";
		}
		//Toast.makeText( getApplicationContext(), locationText, Toast.LENGTH_LONG).show();
		
		//Log.v(TAG, "onLocationChanged - 3");
		
	}
	
	public void onProviderDisabled(String provider)
	{
		//Toast.makeText( getApplicationContext(), "GPS disabled", Toast.LENGTH_SHORT ).show();
	}
	
	public void onProviderEnabled(String provider)
	{
		//Toast.makeText( getApplicationContext(), "GPS enabled", Toast.LENGTH_SHORT ).show();
	}
	
	public void onStatusChanged(String provider, int status, Bundle extras)
	{}
	

	//-------

	private static final int TWO_MINUTES = 1000 * 60 * 2;
	
	/** Determines whether one Location reading is better than the current Location fix
	  * @param location  The new Location that you want to evaluate
	  * @param currentBestLocation  The current Location fix, to which you want to compare the new one
	  */
	protected boolean isBetterLocation(Location location, Location currentBestLocation) 
	{
	    if (currentBestLocation == null) 
	    {
	        // A new location is always better than no location
	        return true;
	    }
	
	    // Check whether the new location fix is newer or older
	    long timeDelta = location.getTime() - currentBestLocation.getTime();
	    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
	    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
	    boolean isNewer = timeDelta > 0;
	
	    // If it's been more than two minutes since the current location, use the new location
	    // because the user has likely moved
	    if (isSignificantlyNewer) 
	    {
	        return true;
	    // If the new location is more than two minutes older, it must be worse
	    } 
	    else if (isSignificantlyOlder) 
	    {
	        return false;
	    }
	
	    // Check whether the new location fix is more or less accurate
	    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
	    boolean isLessAccurate = accuracyDelta > 0;
	    boolean isMoreAccurate = accuracyDelta < 0;
	    boolean isSignificantlyLessAccurate = accuracyDelta > 200;
	
	    // Check if the old and new location are from the same provider
	    boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider());
	
	    // Determine location quality using a combination of timeliness and accuracy
	    if (isMoreAccurate) 
	    {
	        return true;
	    } 
	    else if (isNewer && !isLessAccurate) 
	    {
	        return true;
	    } 
	    else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) 
	    {
	        return true;
	    }
	    return false;
	}
	
	/** Checks whether two providers are the same */
	private boolean isSameProvider(String provider1, String provider2) 
	{
	    if (provider1 == null) 
	    {
	      return provider2 == null;
	    }
	    return provider1.equals(provider2);
	}		
	
}

public class MainActivity extends MapActivity
{
	//http://developer.android.com/resources/tutorials/views/hello-mapview.html
	//http://stackoverflow.com/questions/2176397/drawing-a-line-path-on-google-maps
	
	private static final String TAG = "Footgolfer";

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);
		setContentView(R.layout.user_config);
	}
}


//public class MainActivity extends Activity
//{
//	private static final String TAG = "Footgolfer";
//	private LocationManager _locationManager;
//	private MyLocationListener _locationListener;
//
//	@Override
//	public void onCreate(Bundle savedInstanceState)
//	{
//		super.onCreate(savedInstanceState);
//		// setContentView(R.layout.main);
//		setContentView(R.layout.user_config);
//	}
//	
//
//	public void goToNavigator(View view) 
//	{
//		//showNavigation("TestFer");
//		showNavigation("TestFer", 37.423156,-122.084917);
//	}
//	
//	public void showForm(View view) 
//	{
//		setContentView(R.layout.main);
//	}
//	
//	public void goToHole(View view) 
//	{
//		_locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//		_locationListener = new MyLocationListener();
//		
//		//_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, _locationListener);		
//		_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, _locationListener);		
//		
//		// Remove the listener you previously added
//		//_locationManager.removeUpdates(_locationListener);
//
//		
//		setContentView(R.layout.test);
//		
//	}
//	
//	
//	public void setLocation1(View view) 
//	{
//		try
//		{
//			Log.v(TAG, "setLocation1 - 1");
//			
//			Location loc = getLocation();
//
//			Log.v(TAG, "setLocation1 - 2");
//
//			if ( loc == null )
//			{
//				Log.v(TAG, "setLocation1 - 3");
//				
//				return;
//			}
//			Log.v(TAG, "setLocation1 - 4");
//			
//
//			EditText editLatitude1 = (EditText)findViewById(R.id.editTextLatitude1);
//			
//			Log.v(TAG, "setLocation1 - 5");
//
//			
//			EditText editLongitude1 = (EditText)findViewById(R.id.editTextLongitude1);
//			
//			Log.v(TAG, "setLocation1 - 6");
//
//			
//			editLatitude1.setText(Double.toString(loc.getLatitude()));
//			
//			Log.v(TAG, "setLocation1 - 7");
//
//			
//			editLongitude1.setText(Double.toString(loc.getLongitude()));
//			
//			Log.v(TAG, "setLocation1 - 8");
//
//			
//		}
//		catch (Exception e)
//		{
//			Log.v(TAG, e.toString());
//		}
//	}	
//	
//	public void setLocation2(View view) 
//	{
//		try
//		{
//			Location loc = getLocation();
//			if ( loc == null )
//			{
//				return;
//			}
//			
//			EditText editLatitude2 = (EditText)findViewById(R.id.editTextLatitude2);
//			EditText editLongitude2 = (EditText)findViewById(R.id.editTextLongitude2);
//			editLatitude2.setText(Double.toString(loc.getLatitude()));
//			editLongitude2.setText(Double.toString(loc.getLongitude()));
//		}
//		catch (Exception e)
//		{
//			Log.v(TAG, e.toString());
//		}		
//	}	
//
//	public void calculateDistance(View view) 
//	{
//		_locationManager.removeUpdates(_locationListener);
//		
//		EditText editLatitude1 = (EditText)findViewById(R.id.editTextLatitude1);
//		EditText editLongitude1 = (EditText)findViewById(R.id.editTextLongitude1);
//		EditText editLatitude2 = (EditText)findViewById(R.id.editTextLatitude2);
//		EditText editLongitude2 = (EditText)findViewById(R.id.editTextLongitude2);
//
//		Double lat1 = new Double(editLatitude1.getText().toString());
//		Double lon1 = new Double(editLongitude1.getText().toString());
//		Double lat2 = new Double(editLatitude2.getText().toString());
//		Double lon2 = new Double(editLongitude2.getText().toString());
//		
//		double distance = Misc.distVincenty(lat1, lon1, lat2, lon2);
//		String showText = "Distance Vincenty: " + distance;
//		Toast.makeText( getApplicationContext(), showText ,Toast.LENGTH_SHORT).show();
//		Log.v(TAG, showText);
//
//		
//		
//		distance = Misc.distHaversine(lat1, lon1, lat2, lon2);
//		showText = "Distance Haversine: " + distance;
//		Toast.makeText( getApplicationContext(), showText ,Toast.LENGTH_SHORT).show();
//		Log.v(TAG, showText);
//
//
//		distance = Misc.distHaversine_2(lat1, lon1, lat2, lon2);
//		showText = "Distance Haversine 2: " + distance;
//		Toast.makeText( getApplicationContext(), showText ,Toast.LENGTH_SHORT).show();
//		Log.v(TAG, showText);
//		
//		
//	}	
//
//	
//	
//	public void showLocation1(View view) 
//	{
//		EditText editLatitude1 = (EditText)findViewById(R.id.editTextLatitude1);
//		EditText editLongitude1 = (EditText)findViewById(R.id.editTextLongitude1);
//		Double lat = new Double(editLatitude1.getText().toString());
//		Double lon = new Double(editLongitude1.getText().toString());
//		
//		showNavigation("Location 2", lat, lon);
//	}
//
//	public void showLocation2(View view) 
//	{
//		EditText editLatitude2 = (EditText)findViewById(R.id.editTextLatitude2);
//		EditText editLongitude2 = (EditText)findViewById(R.id.editTextLongitude2);
//		Double lat = new Double(editLatitude2.getText().toString());
//		Double lon = new Double(editLongitude2.getText().toString());
//		
//		showNavigation("Location 2", lat, lon);
//	}		
//	
//	
//	
//	private Location getLocation()
//	{
//		Log.v(TAG, "getLocation - 1");
//		
//		Location returnLoc = _locationListener.getLastKnownLocation();
//		
//		Log.v(TAG, "getLocation - 2");
//		
//		if ( returnLoc == null )
//		{
//			Log.v(TAG, "getLocation - 3");
//			
//			//LocationManager.NETWORK_PROVIDER;
//			returnLoc = _locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);		
//		}
//		
//		Log.v(TAG, "getLocation - 4");
//		
//		return returnLoc;
//	}
//	
//	public void showNavigation(String name, double lat, double lon)
//	{
////		Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
////				Uri.parse("http://maps.google.com/maps?saddr=20.344,34.34&daddr=20.5666,45.345"));
//		
////		Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
////				Uri.parse("google.navigation:q=an+address+city");		
//		
//		
////		Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
////				Uri.parse("geo:0,0?q=37.423156,-122.084917 (" + name + ")")
////			);
//		
//		
//		Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
//				Uri.parse("geo:0,0?q=" + lat + "," + lon + " (" + name + ")")
//			);
//		
//		
//		startActivity(intent);
//	}
//	
//	
//}
